// const express = require('express');
// //2.创建web服务器
// const app = express();
//3.调用app.listen()函数启动服务器
// app.listen(8083, () => {
//     console.log('running at http://127.0.0.1');
// })
// app.get('/', (req, res) => {
//     console.log("get")
// });
// app.post('/', (req, res) => {
//     console.log("res")
// });
const WebSocket = require("ws");
const ws = new WebSocket('ws://127.0.0.1:6700')
const util = require('./util/queryBvInfo')
const { random } = require('./util/random')
const { postMessage } = require('./util/api');
let count = 1;
let obj = {
    group_id: 662907377,
    design: "畜生",
    nickname: "大朗",
    place: "到阴曹地府",
    do: "报到",
    message: function () {
        return `${this.design + this.nickname}速速${this.place + this.do}!`
    },
}
const PIC_MAX = 721;
const createRandomArr = () => {
    return Array.from({ length: PIC_MAX }).map((item, index) => index + 1) || []
}
let randomArr = createRandomArr();
const getRamdomNum = () => {
    let randomArrLen = randomArr.length;
    if (!randomArrLen) {
        randomArr = createRandomArr();
    }
    // console.log("randomArrLen ========>", randomArrLen)
    let randomNum = random(0, randomArrLen - 1);
    // console.log("randomNum ========>", randomNum)
    // console.log("randomArr ========>", JSON.stringify(randomArr))

    let num = randomArr[randomNum]
    randomArr.splice(randomNum, 1)
    // console.log("updater ========>", JSON.stringify(randomArr))
    return num;
}
function videoInfo(data) {
    return `${data.title}
[CQ:image,file=${data.pic},width=400,height=400]
作者：${data.owner.name}
点赞数：${data.stat.like}    点踩数：${data.stat.dislike}
播放数：${data.stat.view}    弹幕数：${data.stat.danmaku}
评论数：${data.stat.reply}   收藏数：${data.stat.favorite}
发布时间：${util.formatDate(data.pubdate)}
简介：${data.desc}
地址：${data.address}`
}


ws.on('open', function open() {
    ws.send("something")
})
ws.on('message', async function message(recData) {
    console.log(JSON.parse(recData).raw_message)
    const data = JSON.parse(recData)
    const isSame = data?.sender?.user_id !== data?.self_id;
    let isGroupAndNotSame = data.message_type == "group" && isSame;
    if (isGroupAndNotSame) {
        if (data.raw_message.indexOf('嗨') != -1) {

        } else if (data.raw_message.indexOf('来斗') != -1) {
            // obj.nickname = "大腰"
            // obj.place = "来嚎哭深渊"
            // let url = `http://127.0.0.1:5700/send_group_msg?group_id=${obj.group_id}&message=${obj.message()}&auto_escape=true`
            // postMessage(url)
        }
        if (data.raw_message === '来点二次元') {
            let msg = `[CQ:image,file=http://123.57.208.2:8088/image/anime/${getRamdomNum()}.jpg]`
            let url = `http://127.0.0.1:5700/send_group_msg?group_id=${data.group_id}&message=${msg}`
            postMessage(url)
        }
    }
    // util.interpaceURL 检测一句话里是否含有http链接。
    let temp = util.interpaceURL(data.raw_message);
    if (temp) {
        // 在链接里提取信息
        let info = util.isBilibiliURL(temp[0])
        const temp1 = await util.getVideoInfo(info.bvid)
        const {
            data: data1
        } = temp1.body
        data1.address = temp[0]
        let msg = videoInfo(data1)
        let url2 = `http://127.0.0.1:5700/send_group_msg?group_id=${data.group_id}&message=${msg}`
        console.log(url2)
        console.log(msg)
        postMessage(url2)

    }



    // request.get({ url, obj }, function (error, response, body) {
    //     console.log(response, body)
    // })

})